<?php
if(!defined('ROOT')){//view目录的上一级目录
	define('ROOT',dirname(dirname(dirname(dirname(__FILE__)))).'/' );
}

ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);

require(ROOT_PATH.'config/config.php');
error_reporting(E_ERROR);
require_once(LIB_PATH."/myframe.php");//框架

require_once "../lib/WxPay.Api.php";
require_once "WxPay.JsApiPay.php";
require_once 'log.php';

//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);

//打印输出数组信息
function printf_info($data)
{
    foreach($data as $key=>$value){
        echo "<font color='#00ff55;'>$key</font> : $value <br/>";
    }
}

function getTradeNo($shopId,$orderId){
	return $shopId.'--'.$orderId.'--'.time();//避免重复支付
}


//①、获取用户openid
$tools = new JsApiPay();
$openId = $tools->GetOpenid();

//②、统一下单
$orderId = intval($_GET['orderId']);
$myOrder = new Order();//我们的订单
$myOrder->find(['orderId',$orderId]);//@todo 检查是否已经支付
$newTradeNo = getTradeNo($myOrder->sellerShopId,$orderId);

$input = new WxPayUnifiedOrder();
$input->SetBody("test Body");
$input->SetAttach("test Attach");
$input->SetOut_trade_no($newTradeNo);
$buyerShouldPay = $myOrder->buyerShouldPay * 100;
$input->SetTotal_fee($buyerShouldPay);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("test");
$input->SetNotify_url("https://likezf.hkbao.com/notify.php");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);//微信的订单
//echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
//printf_info($order);
$jsApiParameters = $tools->GetJsApiParameters($order);

//获取共享收货地址js函数参数
$editAddress = $tools->GetEditAddressParameters();

//③、在支持成功回调通知中处理成功之后的事宜，见 notify.php
/**
 * 注意：
 * 1、当你的回调地址不可访问的时候，回调通知会失败，可以通过查询订单来确认支付是否成功
 * 2、jsapi支付时需要填入用户openid，WxPay.JsApiPay.php中有获取openid流程 （文档可以参考微信公众平台“网页授权接口”，
 * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html）
 */

$modelContent = '';
if(preg_match("/demo/i",$_SERVER['HTTP_HOST'])){//测试期间，始终从远程读取模板
    $url = 'http://demowww.likecto.hkbao.com/jsapi.view.html';
    $template = myspider::fetch($url);
    $reg = "/'\{jsApiParameters\}'/i";
    $template = preg_replace($reg,$jsApiParameters,$template);

    $reg = "/'\{editAddress\}'/i";
    $template = preg_replace($reg,$editAddress,$template);

    $reg = "/'\{buyerShouldPay\}'/i";
    $template = preg_replace($reg,$buyerShouldPay,$template);
    $template .= "<!-- template from $url -->";

    $modelContent = $template;
}else{//正式服务器
    $template = file('jsapi.view.html');
    foreach($template as $line){
        $reg = "/'\{jsApiParameters\}'/i";
        $line = preg_replace($reg,$jsApiParameters,$line);

        $reg = "/'\{editAddress\}'/i";
        $line = preg_replace($reg,$editAddress,$line);

        $reg = "/'\{buyerShouldPay\}'/i";
        $line = preg_replace($reg,$buyerShouldPay,$line);

        $modelContent .= $line;
        $modelContent .= "<!-- template from localfile -->";
    }
}


echo $modelContent;

